<?php
/*
 * Copyright (c) 2008 Sergey Bondari, bondari@bondari.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

namespace Mosaic\NS;


/**
 * Naming and control agent loader
 *
 * @package System
 * @subpackage Nameserver
 */
class Loader {


	/**
	 * Parsed ini file data
	 * @var array
	 */
	private $ini;



	/**
	 *  Class constructor
	 *
	 *  Costructor preload all necessary code to start naming and control agent
	 *
	 * @param string Path to configuration file
	 */
	function __construct($iniFile) {
		if (!file_exists($iniFile)) {
			throw new \Exception('system configuration is missing or not readable.');
		}
		$this->ini = parse_ini_file($iniFile, true);

		$root = $this->ini['general']['path.root'].'/';
		$this->ini['debug']['log.file'] = $root . $this->ini['debug']['log.file'];
		foreach (array('system','lib','ns') as $k) $this->ini['general']['path.'.$k] = $root . $this->ini['general']['path.'.$k];

		$this->loadLib();
		$this->loadNS();
	}


	/**
	 *  Loads library code
	 *
	 * @return void
	 */
	private function loadLib() {
		require_once($this->ini['general']['path.system'].'/constants.php');
		require_once($this->ini['general']['path.system'].'/interfaces.php');
		require_once($this->ini['general']['path.lib'].'/debug.php');
		require_once($this->ini['general']['path.system'].'/exception.remote.php');
		require_once($this->ini['general']['path.system'].'/exception.php');
		require_once($this->ini['general']['path.lib'].'/streams.php');
		require_once($this->ini['general']['path.lib'].'/MCGI/class.mcgi.php');
		require_once($this->ini['general']['path.lib'].'/system/class.generic.request.broker.php');

	}


	/**
	 *  Loads code
	 *
	 * @return void
	 */
	private function loadNS() {
		require_once($this->ini['general']['path.system'].'/class.config.php');
		require_once($this->ini['general']['path.system'].'/class.debug.php');
		/*
		 * Request loading
		 */
		require_once($this->ini['general']['path.system'].'/request/class.request.type.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.php');

		require_once($this->ini['general']['path.system'].'/request/class.request.content.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.nodedown.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.noderesolve.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.nodeup.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.ns.ro.manifest.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.ns.ro.resolve.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.remote.stub.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.ping.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.remote.call.php');
		require_once($this->ini['general']['path.system'].'/request/class.request.remote.instantiate.php');

		require_once($this->ini['general']['path.system'].'/request/class.response.php');

		/*
		 * Nameserver
		 */
		require_once($this->ini['general']['path.ns'].'/class.nameserver.php');
	}
	          	
	/**
	 *  Starts naming and control agent
	 *
	 * @return void
	 */
	public function start() {
		global $sys;
		$sys = new Nameserver(new \Mosaic\System\Config($this->ini));
		$sys->start();
	}
	


}
